package org.jeecg.modules.rental.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.rental.entity.RentalConfig;
import org.jeecg.modules.rental.service.IRentalConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

 /**
 * @Description: 租车配置
 * @Author: sean
 * @Date:   2024-06-20
 * @Version: V1.0
 */
@Api(tags="租车配置")
@RestController
@RequestMapping("/rental/config")
@Slf4j
public class RentalConfigController extends JeecgController<RentalConfig, IRentalConfigService> {

	@Autowired
	private IRentalConfigService rentalConfigService;

	 /**
	  * 分页列表查询
	  * @param page
	  * @param rentalConfig
	  * @return
	  */
	//@AutoLog(value = "租车配置-分页列表查询")
	@ApiOperation(value="租车配置-分页列表查询", notes="租车配置-分页列表查询")
	@GetMapping(value = "/list")
	@PermissionData(pageComponent="rental/config/list")
	public Result<IPage<RentalConfig>> queryPageList(Page page, RentalConfig rentalConfig, HttpServletRequest request) {
		QueryWrapper<RentalConfig> queryWrapper = QueryGenerator.initQueryWrapper(rentalConfig, request.getParameterMap());
		IPage<RentalConfig> pageList = rentalConfigService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param rentalConfig
	 * @return
	 */
	@AutoLog(value = "租车配置-添加")
	@ApiOperation(value="租车配置-添加", notes="租车配置-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody RentalConfig rentalConfig) {
		rentalConfigService.add(rentalConfig);
		return Result.OK("common.add.success");
	}
	
	/**
	 *  编辑
	 *
	 * @param rentalConfig
	 * @return
	 */
	@AutoLog(value = "租车配置-编辑")
	@ApiOperation(value="租车配置-编辑", notes="租车配置-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody RentalConfig rentalConfig) {
		rentalConfigService.edit(rentalConfig);
		return Result.OK("common.edit.success");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "租车配置-通过id删除")
	@ApiOperation(value="租车配置-通过id删除", notes="租车配置-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		rentalConfigService.deleteById(id);
		return Result.OK("common.delete.success");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "租车配置-批量删除")
	@ApiOperation(value="租车配置-批量删除", notes="租车配置-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.rentalConfigService.deleteBatch(Arrays.asList(ids.split(",")));
		return Result.OK("common.delete.batch.success");
	}
	
	/**
	 * 通过id查询
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "租车配置-通过id查询")
	@ApiOperation(value="租车配置-通过id查询", notes="租车配置-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<RentalConfig> queryById(@RequestParam(name="id",required=true) String id) {
		RentalConfig rentalConfig = rentalConfigService.getById(id);
		if(rentalConfig==null) {
			return Result.error("common.query.not.found",null);
		}
		return Result.OK(rentalConfig);
	}

	 /**
	  * 初始化
	  * @return
	  */
	 @AutoLog(value = "租车配置-初始化")
	 @ApiOperation(value="租车配置-初始化", notes="租车配置-初始化")
	 @PostMapping(value = "/init")
	 public Result<String> init() {
		 rentalConfigService.init();
		 return Result.OK("common.add.success");
	 }


    /**
    * 导出excel
    *
    * @param request
    * @param rentalConfig
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, RentalConfig rentalConfig) {
        return super.exportXls(request, rentalConfig, RentalConfig.class, "租车配置");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, RentalConfig.class);
    }


	 /**
	  * 获取二维码前缀
	  * @return
	  */
	 @ApiOperation(value="获取车辆二维码前缀", notes="获取二维码前缀")
	 @GetMapping(value = "/vehicle-qrcode-prefix")
	 public Result<RentalConfig> getVehicleQrCodePrefix() {
		 RentalConfig rentalConfig = rentalConfigService.getVehicleQrCodePrefix();
		 if(rentalConfig==null) {
			 return Result.error("common.query.not.found",null);
		 }
		 return Result.OK(rentalConfig);
	 }


	 /**
	  * 获取二维码前缀
	  * @return
	  */
	 @ApiOperation(value="获取插座二维码前缀", notes="获取二维码前缀")
	 @GetMapping(value = "/socket-qrcode-prefix")
	 public Result<RentalConfig> getSocketQrCodePrefix() {
		 RentalConfig rentalConfig = rentalConfigService.getSocketQrCodePrefix();
		 if(rentalConfig==null) {
			 return Result.error("common.query.not.found",null);
		 }
		 return Result.OK(rentalConfig);
	 }


	 /**
	  * 获取二维码前缀
	  * @return
	  */
	 @ApiOperation(value="获取二维码前缀", notes="获取二维码前缀")
	 @GetMapping(value = "/batch-config")
	 public Result<List<RentalConfig>> batchConfig(String configKeys) {
		 List<RentalConfig> rentalConfigList = rentalConfigService.batchConfig(Arrays.asList(configKeys.split(",")));
		 return Result.OK("equ.rental.config.batch.config", rentalConfigList);
	 }


	 /**
	  * 更具配置key获取配置信息
	  * @return
	  */
	 @ApiOperation(value="更具配置key获取配置信息", notes="更具配置key获取配置信息")
	 @GetMapping(value = "/getConfigByKey")
	 public Result<RentalConfig> getConfigByKey(String configKey, String orgCode) {
		 RentalConfig rentalConfig = rentalConfigService.get(configKey);
		 return Result.OK("success", rentalConfig);
	 }


}
